﻿--[[
   @brief    login_popup_tips_control
   @author   maxyao, charleycao
   @version  1.0
   @date     2017-03-23
--]]

login_popup_tips_control =
{
    init = function()
        ierd_tgp.tpf_ui.Require("login_popup_tips\\view.lua");
        ierd_tgp.tpf_ui.Require("login_popup_tips\\model.lua");
    end;
    
    uninit = function()
    end;

    close_tips = function(data)
        if login_popup_tips_model.window_exists then
            login_popup_tips_view.close();
            login_popup_tips_control.report_operation(3, 0, login_popup_tips_model.tips_type_id);
            login_popup_tips_control.report_ad_system_batch(login_popup_tips_model.ad_system_report_data);
            login_popup_tips_model.ad_system_report_data = nil; -- 清空上报数据
            login_popup_tips_model.window_exists = false;
        end
    end;

    on_js_callback = function(uilib, fuc_name, json)
        base_utility.log("[lptc] on_js_callback.fuc_name=" .. fuc_name, true);
        if fuc_name ==  "wegame_ad_index_report" then
            local ad_data = base_utility.json_to_table(json);
            local ad_curr = ad_data.data.report_arr[1].idx;
            local ad_count = ad_data.data.report_arr[1].count;
            login_popup_tips_model.tips_page_curr = ad_curr;
            login_popup_tips_model.tips_page_count = ad_count;
            login_popup_tips_view.update_native_dlg();
        elseif fuc_name == "close_window" then
            login_popup_tips_control.close_tips();       
        elseif fuc_name == "wegame_load_picture" then
            -- 加载完毕才正常显示广告页面
            if login_popup_tips_model.game_running_state == 0 then
                local is_launcher = base_utility.is_type_of_launcher();
                if is_launcher == true and 
                   login_popup_tips_model.focus_banner_ ~= GAME_ID.GAME_ID_SHOP then
                    login_popup_tips_model.waiting_login_tips_show = 1;
                    base_utility.log(
                        "[lptc]not in shop banner, wait really show wnd", true);
                else
                    login_popup_tips_model.waiting_login_tips_show = nil;
                    login_popup_tips_control.really_show_wnd();
                end;                
            else
                login_popup_tips_model.waiting_login_tips_show = 1;
                base_utility.log("[lptc]game is running, wait really show wnd", true);
            end
        elseif fuc_name == "wegame_load_picture_fail" then
            local ad_data = base_utility.json_to_table(json);  
            local src = ad_data.data.report_arr[1].src;    
            base_utility.log("[lptc] wegame_load_picture_fail: ".. src); 
            login_popup_tips_control.close_tips();
        else
            base_utility.log("[lptc] on_js_callback other event!");
        end;
    end;


    get_login_popup_tips_guide_flag = function() -- user operation guide flag
        local res = comm_center.call_service("i_service_get_login_popup_tips_guide_flag", data);
        local flag = res.flag;
        if res ~= nil and res.flag ~= nil then
            if res.flag == 0 then
                login_popup_tips_model.need_show_login_popup_tips_guide = true;
            else
                login_popup_tips_model.need_show_login_popup_tips_guide = false;
            end;
        end;
    end;

    set_login_popup_tips_guide_flag = function(flag)
        local result_table = comm_center.call_service("i_service_set_login_popup_tips_guide_flag", {flag = flag});
    end;

    on_finsh_wegame_user_guide = function(data)
        if data == nil or data.show == nil then
            base_utility.log("[lptc]on_finsh_wegame_user_guide, param nil", true);
            return;
        end;
        if login_popup_tips_model.has_user_guide_ ~= nil then
            base_utility.log("[lptc]on_finsh_wegame_user_guide, has_user_guide_ not empty", true);
            return;
        end;
        base_utility.log("[lptc]on_finsh_wegame_user_guide: "..tostring(data.show), true);
        login_popup_tips_model.has_user_guide_ = data.show;
        login_popup_tips_control.check_show_tips();
    end;

    check_show_tips = function()
        if login_popup_tips_model.has_user_guide_ == nil or
           login_popup_tips_model.has_user_guide_ == true then
            base_utility.log("[lptc]check_show_tips, has_user_guide_ not match", true);
            return;
        end;

        if login_popup_tips_model.rsp_tips_data_ == nil then
            base_utility.log("[lptc]check_show_tips, rsp_tips_data_  empty", true);
            return;
        end;
        login_popup_tips_control.show_login_popup_tips(login_popup_tips_model.rsp_tips_data_);
        login_popup_tips_model.rsp_tips_data_ = nil;
    end;

    on_brc_show_login_popup_tips = function(data)
        base_utility.log("[lptc]on_brc_show_login_popup_tips", true);
        login_popup_tips_model.rsp_tips_data_ = data;
        login_popup_tips_control.check_show_tips();
    end;

    show_login_popup_tips = function(tips_data)
        base_utility.log("[lptc]show_login_popup_tips", true);
        if tips_data == nil or tips_data.content_json_str == nil then 
            base_utility.log("[lptc]show_login_popup_tips failed, data is nil", true);
            return;
        end;

        -- 判断是否是launcher启动的wegaeme，如果是，则不响应显示登录浮层
        --[[local is_launcher = base_utility.is_type_of_launcher();
        if is_launcher == true then
            base_utility.log("we game is launched by game, not show login popup tips");
            return;
        end;--]]

        -- 黑名单用户不弹浮层
        local blacklist_tbl = comm_center.call_service("Svr_QueryIsBlackListUser", {});
        if blacklist_tbl ~= nil and blacklist_tbl.is_blacklist_user then
            base_utility.log("[lptc]is blacklist user, not show login popup tips", true);
            return;
        end

        if login_popup_tips_model.game_running_state == 0 then -- no game running
            login_popup_tips_model.waiting_login_tips_data = nil;

            login_popup_tips_control.get_login_popup_tips_guide_flag(); -- tips guide flag
            local setcount = math.min(tips_data.count, login_popup_tips_model.max_show_count);
            login_popup_tips_model.tips_page_count = setcount;
            login_popup_tips_model.tips_total_count = setcount;
            login_popup_tips_model.uin = tonumber(tips_data.uin);
            local data_table = base_utility.json_to_table(tips_data.content_json_str);
            if login_popup_tips_model ~= nil 
                and data_table ~= nil and data_table["parent_id"] == tostring(login_popup_tips_model.tips_type_id) then
                
                local tips_content = data_table["child_list"];
                login_popup_tips_model.tips_content = login_popup_tips_model.reconstruct_content(tips_content); -- page data

                login_popup_tips_view.ready();      -- 加载网页而不显示

                base_utility.log("[lptc]tips wnd ready", true);

                if login_popup_tips_model.show_directly then
                    base_utility.log("[lptc]directly show for debug", true);
                    login_popup_tips_view.show();   -- 用于调试
                end
            else
                base_utility.log("[lptc]show_login_popup_tips failed, content is nil");
            end;
        else -- exist running state, save tips data, only one.
            login_popup_tips_model.waiting_login_tips_data = tips_data;
            base_utility.log("[lptc]game is running, save waiting tips data", true);
        end;
    end;

    get_login_popup_tips_data = function()
        if login_popup_tips_model ~= nil then
            local param = {};
            param["tips_type_id"] = login_popup_tips_model.tips_type_id;
            param["is_show_guide"] = login_popup_tips_model.need_show_login_popup_tips_guide;
            param["page_count"] = login_popup_tips_model.tips_total_count;
            param["content"] = login_popup_tips_model.tips_content;
            param["uin"] = login_popup_tips_model.uin;
            param["switch_time"] = login_popup_tips_model.switch_time;
            return param;
        else
            base_utility.log("[lptc]get_login_popup_tips_data is nil");
        end;
    end;

    report_ad_system = function (data)
        if data ~= nil and data.occasion ~= nil and data.report_info ~= nil then
            local occasion = data.occasion;
            local report_info = data.report_info;
            local key = base_utility.table_to_json(report_info);
            if occasion == 0 then
                -- 即时上报
                login_popup_tips_control.report_ad_system_batch({[key] = report_info}); 
            else
                -- 退出上报，缓存数据
                if login_popup_tips_model.ad_system_report_data == nil then
                    login_popup_tips_model.ad_system_report_data = {};
                end
                if login_popup_tips_model.ad_system_report_data[key] == nil then
                    login_popup_tips_model.ad_system_report_data[key] = report_info;
                end
            end
            base_utility.log(
        "[lptc][report_ad_system] Report data: occsaion = " .. occasion .. " report_info = ".. key);
        end
    end;

    report_ad_system_batch = function (data_array)
        if data_array == nil then
            return;
        end
        local tips_table = login_popup_tips_model.tips_content;
        if tips_table == nil then
            base_utility.log("[lptc][report_ad_system_batch] tips_table invalid");
            return;
        end; 

        local expose_array = {};
        local like_array = {};
        local click_array = {};
        local need_report_expose = false;
        local need_report_like = false;
        local need_report_click = false;

        for key, data in pairs(data_array) do
            if data ~= nil then
                local pos_id = tostring(data.pos_id or 5000);
                local report_type = data.report_type or "";
                local action = data.action or -1;
            
                local ad_data = tips_table[pos_id];    
                if ad_data ~= nil then 
                    local report_data = {};
                    report_data.task_id = tonumber(ad_data.task_id) or 0;
                    report_data.pos_id = tonumber(ad_data.pos_id) or 0;
                    report_data.ad_id = tonumber(ad_data.ad_id) or 0;
                    report_data.game_id = tonumber(ad_data.game_id) or 0;
                    
                    if report_type == "expose" then
                        table.insert(expose_array, report_data);
                        need_report_expose = true;
                    elseif report_type == "like" then
                        report_data.like_or_not = tonumber(action);
                        table.insert(like_array, report_data);
                        need_report_like = true;
                    elseif report_type == "click" then
                        table.insert(click_array, report_data);
                        need_report_click = true;
                    end
                end
            end
        end

        if need_report_expose then
            -- 曝光上报
            local expose_data = {expose_info_list = expose_array};
            local url = login_popup_tips_model.ad_expose_url .."?p="..base_utility.table_to_json(expose_data);
            base_utility.log("[lptc][report_ad_system_batch] Expose report, url:" .. url);
            --url = web_utility.make_login_status_url(url, TLoginModule.GetLoginUin());
            base_utility.curl_request_async(url, "login_popup_tips_control.on_report_expose_ad_tips", true, nil, true);
        end

        if need_report_like then
            -- 喜欢不喜欢上报，暂时不支持批量上报
            for i, like_data in ipairs(like_array) do
                 local url = login_popup_tips_model.ad_like_url .."?p="..base_utility.table_to_json(like_data);
                 base_utility.log("[lptc][report_ad_system_batch] Like report url:" .. url);
                 --url = web_utility.make_login_status_url(url, TLoginModule.GetLoginUin());
                 base_utility.curl_request_async(url, "login_popup_tips_control.on_report_like_ad_tips", true, nil, true);
            end
        end

        if need_report_click then
            -- 点击上报
            local click_data = {click_info_list = click_array};
            local url = login_popup_tips_model.ad_click_url .."?p="..base_utility.table_to_json(click_data);
            base_utility.log("[lptc][report_ad_system_batch] Click report, url:" .. url);
            --url = web_utility.make_login_status_url(url, TLoginModule.GetLoginUin());
            base_utility.curl_request_async(url, "login_popup_tips_control.on_report_click_ad_tips", true, nil, true);
        end
    end;

    on_report_expose_ad_tips = function (json_str)
        base_utility.log("[lptc][on_report_expose_ad_tips] expose_ad_tips: " .. json_str);
    end;

    on_report_like_ad_tips = function (json_str)
        base_utility.log("[lptc][on_report_like_ad_tips] like_ad_tips: " .. json_str);
    end;

    on_report_click_ad_tips = function (json_str)
        base_utility.log("[lptc][on_report_click_ad_tips] click_ad_tips: " .. json_str);
    end;

    get_game_runing_state = function (event_id, param)
        local table_data = base_utility.json_to_table(param);
        if table_data ~= nil and table_data.running_state ~= nil and login_popup_tips_model ~= nil then
            login_popup_tips_model.game_running_state = table_data.running_state; -- sync game state 
            base_utility.log("[lptc][get_game_runing_state] game state is "
                .. login_popup_tips_model.game_running_state, true);
        end;

        if login_popup_tips_model.game_running_state == 0 then
            if login_popup_tips_model.waiting_login_tips_data ~= nil then
                base_utility.log("[lptc]no game running, try show waiting tips data", true);
                login_popup_tips_control.show_login_popup_tips(
                    login_popup_tips_model.waiting_login_tips_data);
            end

            if login_popup_tips_model.waiting_login_tips_show ~= nil then
                base_utility.log("[lptc]no game running, try show waiting tips wnd", true);
                local is_launcher = base_utility.is_type_of_launcher();
                if is_launcher == true and 
                   login_popup_tips_model.focus_banner_ ~= GAME_ID.GAME_ID_SHOP then
                    base_utility.log("[lptc]not in shop page, not show waiting tips wnd", true);
                    return;
                else
                    login_popup_tips_model.waiting_login_tips_show = nil;
                    login_popup_tips_control.really_show_wnd();
                end;
            end
        end;
    end;

    game_ended_show_login_popup_tips = function (event_id, param)
    end;

    report_operation = function(op_type, game_id, tips_id)
        local qosparam = ierd_tgp.tpf_ui.LuaQosData:new();
        qosparam.oc_type = ierd_tgp.tpf_ui.kInstant;
        qosparam.qos_report_id = ierd_tgp.adapt_for_imports.qos.Qos_kind.kTipState; -- 6688
        
        qosparam.int_val[0] = op_type;
        qosparam.int_count = 1;

        qosparam.str_val1 = tostring(tips_id);
        qosparam.str_val2 = "login_popup_tips";
        qosparam.str_count = 2;
        base_utility.lua_qos(qosparam);
        qosparam:delete();
    end;

    on_focus_banner_item = function(event_id, json_str)
        local is_launcher = base_utility.is_type_of_launcher();
        if is_launcher ~= true then
            return;
        end;

        local param = base_utility.json_to_table(json_str);
        local game_id = 0;
        if param ~= nil and param.module_id ~= nil then
            game_id = tonumber(param.module_id);
        end;
        if game_id == 0 then
            return;
        end;
        login_popup_tips_model.focus_banner_ = game_id;

        if GAME_ID.GAME_ID_SHOP ~= game_id then
            return;
        end;
        
        if login_popup_tips_model.game_running_state == 0 and
           login_popup_tips_model.waiting_login_tips_show ~= nil then
            base_utility.log("[lptc]on focus shop banner, show waiting tips wnd", true);
            login_popup_tips_model.waiting_login_tips_show = nil;
            login_popup_tips_control.really_show_wnd();
        end
    end;

    really_show_wnd = function()
        base_utility.log("[lptc]really_show_wnd", true);
        login_popup_tips_view.show();
        comm_center.send_broadcast(login_popup_tips_model.finish_initilization_broadcast, {});
        login_popup_tips_control.report_operation(1, 0, login_popup_tips_model.tips_type_id);
    end;
    
};

login_popup_tips_control.init();